package com.jar.exercise.多线程.AQS.LockSupport.线程交替;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {
    public static int num = 1;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.execute(new MyImplement(0));
        executorService.execute(new MyImplement(1));
        executorService.execute(new MyImplement(2));
    }

}
class MyImplement implements Runnable{
//    private AtomicInteger count;
    private static volatile AtomicInteger count=new AtomicInteger(0);

    private int num;
    public MyImplement(AtomicInteger i,int num) {
        this.count = i;
        this.num = num;
    }
    public MyImplement(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (true) {
            if (count.get() > 100) {
                break;
            }
            if (count.get() % 3 == num) {
                System.out.println(Thread.currentThread().getName()+":::"+count);
                count.incrementAndGet();
            }
        }
    }
}